﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Web;
using System.Web.Mvc;
using MiniStock.Client.UI.CommandService;
using MiniStock.Client.UI.Models;
using MiniStock.Client.UI.QueryService;

namespace MiniStock.Client.UI.Controllers
{
    public class CustomerController : Controller
    {
        //
        // GET: /Customer/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Create()
        {
            var model = new CreateCustomerModel
                            {
                                Name = "Béla",
                            };
            return View(model);
        }

        public ActionResult Get(Guid id)
        {
            var service = new StockServiceFacadeClient();
            var result = service.GetCustomer(id);
            return View(result);
        }

        public ActionResult List()
        {
            var service = new StockServiceFacadeClient();
            var result = service.ListCustomers(null);
            return View(result);
        }

        public ActionResult Search(string name)
        {
            var service = new StockServiceFacadeClient();
            var queryToken = new QueryToken()
                                 {
                                     Query = new List<FilterElement>(),
                                 OrderBy = "Name",
                                     PageSize = 100,
                                     PageIndex = 0,
                                 };
            if(!String.IsNullOrEmpty(name))
                queryToken.Query.Add(
                    new FilterElement
                        {FieldName = "Name", Operator = QueryOperator.Like, FieldValue = String.Format("'%{0}%'", name)});

            var result = service.ListCustomers(queryToken);
            return Json(result.Select(i =>
                                          new {
                                              value = i.Id,
                                              label = i.Name,
                                          }).ToList(), JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public Guid CreateCustomer(string name)
        {
            var service = new ServiceFacadeClient();
            var result = service.SubmitEvent(new CreateCustomerEvent
                                    {
                                        Id = Guid.NewGuid(),
                                        EventId = Guid.NewGuid(),
                                        Name = name,
                                    });

            return result;
        }

        public ActionResult ChangeName(Guid id)
        {
            var service = new StockServiceFacadeClient();
            var result = service.GetCustomer(id);
            var model = new ChangeNameModel(result);
            
            return View(model);
        }

        [HttpPost]
        public Guid ChangeName(ChangeNameModel model)
        {
            var service = new ServiceFacadeClient();
            var result = service.SubmitEvent(new ChangeCustomerNameEvent
                                    {
                                        EventId = Guid.NewGuid(),
                                        Id = model.Id.Value,
                                        EventDate = DateTime.UtcNow,
                                        Name = model.Name,
                                    });
            return result;
        }

        public ActionResult ChangeAddress(Guid id, string addressType)
        {
            var service = new StockServiceFacadeClient();
            var result = service.GetCustomer(id);
            ChangeAddressModel model;
            if (addressType == "HQ") model = new ChangeAddressModel(result, result.HQAddress);
            else if (addressType == "Shipping") model = new ChangeAddressModel(result, result.ShippingAddress);
            else throw new ApplicationException("Unknown address type");

            return View("ChangeAddress", model);
        }

        [HttpPost]
        public Guid ChangeAddress(string addressType, ChangeAddressModel model)
        {
            var service = new ServiceFacadeClient();
            AddressChangeEvent e;
            if (addressType == "HQ") e = new ChangeHQAddressEvent();
            else if (addressType == "Shipping") e = new ChangeShippingAddressEvent();
            else throw new ApplicationException("Unknown address type");

            e.EventId = Guid.NewGuid();
            e.Id = model.Id.Value;
            e.EventDate = DateTime.UtcNow;
            e.City = model.City;
            e.State = model.State;
            e.StreetName = model.StreetName;
            e.StreetNumber = model.StreetNumber;

            var result = service.SubmitEvent(e);
            return result;
        }

        public ActionResult ChangeContactPerson(Guid id)
        {
            var service = new StockServiceFacadeClient();
            var result = service.GetCustomer(id);
            var model = new ChangeContactPersonModel(result, result.ContactPerson);

            return View(model);
        }

        [HttpPost]
        public Guid ChangeContactPerson(ChangeContactPersonModel model)
        {
            var service = new ServiceFacadeClient();
            var result = service.SubmitEvent(new ChangeContactPersonEvent
            {
                EventId = Guid.NewGuid(),
                Id = model.Id.Value,
                EventDate = DateTime.UtcNow,
                Email = model.Email,
                FirstName = model.FirstName,
                LastName = model.LastName,
                Phone = model.Phone,
            });
            return result;
        }


    }
}
